package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.PathSectionCompleteAdapter;
import com.runen.infinitemovement.model.entity.PathSectionCompleteEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class PathSectionCompletePopupWindow extends PopupWindow {
    private ImageView iv_dismiss;
    private Context context;
    private View layout;
    private PathSectionCompleteEntity.DataBean entity;
    private List<PathSectionCompleteEntity.DataBean> list;
    private RecyclerView rcv_content;
    private PathSectionCompleteAdapter adapter;
    private DismissClick dismissClick;

    public static PathSectionCompletePopupWindow getInstance(Context context, PathSectionCompleteEntity.DataBean entity, DismissClick dismissClick) {
        return new PathSectionCompletePopupWindow(context, entity, dismissClick);
    }

    private PathSectionCompletePopupWindow(Context context, PathSectionCompleteEntity.DataBean entity, DismissClick dismissClick) {
        super(context);
        this.context = context;
        this.entity = entity;
        this.dismissClick = dismissClick;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(false);
        this.setOutsideTouchable(false);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);

        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_path_section_complete, null);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        rcv_content = layout.findViewById(R.id.rcv_content);

        list = new ArrayList<>();
        if (entity.myRank != 0) {
            PathSectionCompleteEntity.DataBean dataBean = new PathSectionCompleteEntity.DataBean();
            dataBean.itemType = PathSectionCompleteAdapter.TYPE_COMPLETE;
            dataBean.myRank = entity.myRank;
            dataBean.startPoint = entity.startPoint;
            dataBean.endPoint = entity.endPoint;
            dataBean.tinderNumber = entity.tinderNumber;
            list.add(dataBean);
        }
        if (!TextUtils.isEmpty(entity.sectionMedalUrl)) {
            PathSectionCompleteEntity.DataBean dataBean = new PathSectionCompleteEntity.DataBean();
            dataBean.itemType = PathSectionCompleteAdapter.TYPE_MEDAL;
            dataBean.sectionMedalUrl = entity.sectionMedalUrl;
            dataBean.startPoint = entity.startPoint;
            dataBean.endPoint = entity.endPoint;
            list.add(dataBean);
        }
        if (!entity.prizeReceiveStatus && !TextUtils.isEmpty(entity.prizeName) && !TextUtils.isEmpty(entity.prizeUrl)) {
            PathSectionCompleteEntity.DataBean dataBean = new PathSectionCompleteEntity.DataBean();
            dataBean.itemType = PathSectionCompleteAdapter.TYPE_PRIZE;
            dataBean.prizeName = entity.prizeName;
            dataBean.startPoint = entity.startPoint;
            dataBean.endPoint = entity.endPoint;
            dataBean.prizeUrl = entity.prizeUrl;
            dataBean.pathId = entity.pathId;
            dataBean.sectionId = entity.sectionId;
            list.add(dataBean);
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        rcv_content.setLayoutManager(linearLayoutManager);
        adapter = new PathSectionCompleteAdapter(context, list, new PathSectionCompleteAdapter.Click() {
            @Override
            public void shareClick() {
                SharePoppupWindow sharePoppupWindow = SharePoppupWindow.getInstance(context, 3, entity);
                sharePoppupWindow.setOnClick(new SharePoppupWindow.OnClick() {
                    @Override
                    public void savePic(Bitmap bitmap) {

                    }

                    @Override
                    public void hitCard() {

                    }

                    @Override
                    public void dismiss() {
                        show();
                    }

                    @Override
                    public String imagePath(Bitmap bitmap) {
                        return null;
                    }
                });
                sharePoppupWindow.show();
                dismiss();
                WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
                lp.alpha = 0.5f;
                ((Activity) context).getWindow().setAttributes(lp);
            }
        });
        rcv_content.setAdapter(adapter);

        PagerSnapHelper snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(rcv_content);
    }


    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dismissClick != null)
                    dismissClick.click();
                dismiss();
            }
        });
    }


    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public interface DismissClick {
        void click();
    }
}
